package business.managers;

import java.rmi.RemoteException;
import java.util.ArrayList;

import Articulo.src.business.entities.*;
import Articulo.src.service.dataBase.*;
import Articulo.src.business.entities.*;
import Articulo.src.service.exceptions.*;
import Articulo.src.*;
/*
import service.dataBase.BusinessConector;
import service.exceptions.DBWritingException;
import service.exceptions.EmptyDBException;
import service.exceptions.NotHereException;
import business.interfaces.ArticleMgt;
import business.entities.*;
<<<<<<< .mine
import Articulo.src.business.*;
import Articulo.src.business.entities.Ingredient;
import Articulo.src.business.interfaces.ArticleMgt;
import Articulo.src.*;
*/


public class ArticleMgr implements ArticleMgt{
	
	private Article[] articulos;

	public void addArticle(String name, String desc, int price,
			String ingr, String cat) throws RemoteException, DBWritingException {
		Ingredient[] aux = this.armarIng(ingr);
		BusinessConector con = new BusinessConector();
		con.articleToDB(new Article(name,desc,price,aux,new Category(cat)));
		Article[] articleAux = new Article[this.articulos.length+1];
		System.arraycopy(articulos, 0, articleAux, 0, articulos.length);
		articulos = articleAux;
		
	}


	public void deleteArticle(Article toDelete) throws RemoteException, DBWritingException {
		BusinessConector con = new BusinessConector();
		con.deleteArticle(toDelete.getPrimaryKey());		
	}

	
	public Article[] getArticles() throws RemoteException {
		Article a = new Article("hola", "b", 3, null,null );
		
			Article[] art = new Article[1];
		art[0]=a;
		return art;
	}
	
	public ArticleMgr() throws EmptyDBException{
		BusinessConector con = new BusinessConector();
		this.articulos =  con.getAllArticles();
	}
	
	private Ingredient[] armarIng(String ingr){
		String[] aux = ingr.split(" ");
		ArrayList<Ingredient> lista = new ArrayList<Ingredient>();
		for(String e : aux){
			lista.add(new Ingredient(e));
		}
		return lista.toArray(new Ingredient[0]);		
	}


	
	public void modifyArticle(String name, String desc, int price, String ingr,
			String cat, int primaryKey) throws RemoteException, DBWritingException, NotHereException {
		BusinessConector con = new BusinessConector();
		con.deleteArticle(primaryKey);
		con.articleToDB(new Article(name, desc, price, this.armarIng(ingr), new Category(cat)));
				
	}
	
	

}
